package org.linlinjava.litemall.db.service;

import com.github.pagehelper.PageHelper;
import org.linlinjava.litemall.db.dao.LitemallBrandNewMapper;
import org.linlinjava.litemall.db.domain.LitemallBrand;
import org.linlinjava.litemall.db.domain.LitemallBrandNew;
import org.linlinjava.litemall.db.domain.LitemallBrandNewExample;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author zhy
 * @date 2019-07-22 19:05
 **/
@Service
public class LitemallNewBrandService {
	@Resource
	private LitemallBrandNewMapper brandNewMapper;

	public List<LitemallBrandNew> query(int offset, int limit) {
		LitemallBrandNewExample example = new LitemallBrandNewExample();
		example.or().andDeletedEqualTo(false);
		example.orderBy(LitemallBrand.Column.addTime.desc());
		PageHelper.startPage(offset, limit);
		return brandNewMapper.selectByExample(example);
	}

	public int queryTotalCount() {
		LitemallBrandNewExample example = new LitemallBrandNewExample();
		example.or().andDeletedEqualTo(false);
		return (int)brandNewMapper.countByExample(example);
	}

	public LitemallBrandNew findById(Integer id) {
		return brandNewMapper.selectByPrimaryKey(id);
	}

	public List<LitemallBrandNew> querySelective(String id, String name, Integer page, Integer size, String sort, String order) {
		LitemallBrandNewExample example = new LitemallBrandNewExample();
		LitemallBrandNewExample.Criteria criteria = example.createCriteria();

		if (!StringUtils.isEmpty(id)) {
			criteria.andIdEqualTo(Integer.valueOf(id));
		}
		if (!StringUtils.isEmpty(name)) {
			criteria.andNameLike("%" + name + "%");
		}
		criteria.andDeletedEqualTo(false);

		PageHelper.startPage(page, size);
		return brandNewMapper.selectByExample(example);
	}

	public List<LitemallBrandNew> queryTotalSelective(String id, String name, String sort, String order) {
		LitemallBrandNewExample example = new LitemallBrandNewExample();
		LitemallBrandNewExample.Criteria criteria = example.createCriteria();

		if (!StringUtils.isEmpty(id)) {
			criteria.andIdEqualTo(Integer.valueOf(id));
		}
		if (!StringUtils.isEmpty(name)) {
			criteria.andNameLike("%" + name + "%");
		}
		criteria.andDeletedEqualTo(false);

		return brandNewMapper.selectByExample(example);
	}

	public int countSelective(String id, String name, Integer page, Integer size, String sort, String order) {
		LitemallBrandNewExample example = new LitemallBrandNewExample();
		LitemallBrandNewExample.Criteria criteria = example.createCriteria();

		if (!StringUtils.isEmpty(id)) {
			criteria.andIdEqualTo(Integer.valueOf(id));
		}
		if (!StringUtils.isEmpty(name)) {
			criteria.andNameLike("%" + name + "%");
		}
		criteria.andDeletedEqualTo(false);

		return (int)brandNewMapper.countByExample(example);
	}

	public void updateById(LitemallBrandNew brand) {
		brandNewMapper.updateByPrimaryKeySelective(brand);
	}

	public void deleteById(Integer id) {
		brandNewMapper.logicalDeleteByPrimaryKey(id);
	}

	public void add(LitemallBrandNew brand) {
		brandNewMapper.insertSelective(brand);
	}

	public List<LitemallBrandNew> all() {
		LitemallBrandNewExample example = new LitemallBrandNewExample();
		return brandNewMapper.selectByExample(example);
	}

	public List<LitemallBrandNew> alls() {
		LitemallBrandNewExample example = new LitemallBrandNewExample();
		example.or().andDeletedEqualTo(false);
		return brandNewMapper.selectByExample(example);
	}
}
